﻿#ifndef LR1_PARSERS_H
#define LR1_PARSERS_H

#include "LanguageTypes.h"
#include "LanguageParsers.h"
#include "StateTransformTable.h"
#include "States.h"


//定义LR1分析器类LR1Parsers
class LR1Parsers : public LanguageParsers
{
public:
	LR1Parsers(Compiler* compiler) : LanguageParsers(compiler) { this->isInitialized = false; }
    bool Initialize();
	virtual void DisplayStates();
    //保存语法分析器状态到文件
    virtual bool Save(ostream& o);
    //从文件加载语法分析器状态
    virtual bool Load(istream& i);
    virtual string GetParserType() { return "lr"; }
    virtual void Show() {
        std::cerr<<"state_transform_table:"<<std::endl;
        std::cerr<<this->state_transform_table<<std::endl;
        std::cerr<<"state_sets:"<<std::endl;
        std::cerr<<this->state_sets<<std::endl;
    }
	//根据文法规则对输入符号流进行文法结构的分析,分析结果用文法分析树表示
	virtual bool Parse(SymbolsFlow& input_symbols, LanguageTree*& source_tree, Symbols start_symbol);
//重载LRPARSERS支撑算法
protected:
	//判定文法是否存在移近规约冲突（文法二义性判断）
	virtual bool IsAmbiguous(const vector<vector<TransformEdge>> &state_transform_table, const vector< StateSets<LR1States> >& state_sets);
private:
	StateTransformTable state_transform_table;
	vector< StateSets<LR1States> > state_sets;
    bool isInitialized;
    map<Symbols,::size_t> entries;
private:
    void SaveStates(ostream &o);
    void LoadStates(istream &i);
    void SaveTransformTable(ostream& o);
    void LoadTransformTable(istream &i);
    void SaveEntries(ostream &o);
    void LoadEntries(istream &i);
};//End of class LR1Parsers

#endif  //LR1_PARSERS_H

